<?php

include_once '../Interfaces/XMLInterface.php';
include_once '../InfoManager/CarSharingManager.php';
include_once '../Exceptions/CommunicationException.php';
include_once '../InfoManager/Constants.php';
include_once '../Utility/BaseGeocode.php';
include_once '../Utility/GoogleGeocode.php';

 /**
 * La classe CarSharingParking rappresenta i parcheggi del carsharing
 * @author Matteo Nati
 *
 */
class CarSharingParking implements XMLInterface {

	private $parkingName;//nome del parcheggio
	private $address;//indirizzo
	private $city;//citta
	private $lat;//latitudine
	private $long;//longitudine
	private $total; //posti totali
	/*private $available;//posti disponibili*/
	
	private $elementStart;
	private $elementEnd;
	
	private $input;//input fornito dall'utente
	
	/**
	* Costruttore per gli oggetti di classe CarSharingParking
	* @param String $name stringa che indica il nome del parcheggio
	* @return BikeStop un oggetto di classe BikeStop
	*/
	function __construct($name){

		$this->input = $name;

		$this->fillCarSharingParking();

		$this->elementStart = "<CARSHARING>";
		$this->elementEnd = "</CARSHARING>";	
	}

	/**
	* Funzione che riempie l'oggetto CarSharingParking (se l'oggetto esiste)
	*/
	private function fillCarSharingParking() {
		
		$cm = new CarSharingManager();
		$info = $cm->getCarSharingParkingInfo($this->input);
				
		if ($info!="")
		{
			$parts = explode(CarSharingManager::$INFOSEPARATOR, $info);
			$this->parkingName = $parts[0];
			$this->address = $parts[1];
			$this->city = $parts[2];
			$this->lat = $parts[3];
			$this->long = $parts[4];
			$this->total = $parts[5];
		}
		
		unset($cm);
		unset($info);
	}
	
	function getAddress(){
		
		return $this->address;
	}
	
	/**
	* Sintetizza l'oggetto in xml.
	* @see src/Interfaces/XMLInterface#toXML()
	*/
	function toXML($mode) {

		if ($mode == EXTENDED_MODE){//restituisce il codice XML completo di tag iniziali e finali

			$xml = "";
			$xml = "<PARKINGNAME>".$this->parkingName."</PARKINGNAME>\n";
			$xml = $xml."<ADDRESS>".$this->address."</ADDRESS>\n";
			$xml = $xml."<CITY>".$this->city."</CITY>\n";
			$xml = $xml."<LAT>".$this->lat."</LAT>\n";
			$xml = $xml."<LONG>".$this->long."</LONG>\n";
			$xml = $xml."<TOTAL>".$this->total."</TOTAL>\n";
			$xml = "\n".$this->elementStart."\n<CARPARKINGLIST>\n<CARPARKING>\n".$xml."</CARPARKING>\n</CARPARKINGLIST>\n".$this->elementEnd."\n";
			}

		if ($mode == COMPACT_MODE){//restituisce il codice XML relativo al parcheggio

			$xml = "<PARKINGNAME>".$this->parkingName."</PARKINGNAME>\n";
			$xml = $xml."<ADDRESS>".$this->address."</ADDRESS>\n";
			$xml = $xml."<CITY>".$this->city."</CITY>\n";
			$xml = $xml."<LAT>".$this->lat."</LAT>\n";
			$xml = $xml."<LONG>".$this->long."</LONG>\n";
			$xml = $xml."<TOTAL>".$this->total."</TOTAL>\n";

			$xml = "<CARPARKING>\n".$xml."</CARPARKING>\n";
			}

			return $xml;	
		}
		
	function getGPS(){
		
		/*
		 //crea oggetto GPS a partire da un'indirizzo
		
		$geo = new GoogleGeocode(apiKey);
		$stringa = utf8_encode("$this->address, $this->city, Italia");
		$coord = $geo->geocode($stringa);
		
		$latVal = $coord['Placemarks'][0]['Latitude'];
		$longVal = $coord['Placemarks'][0]['Longitude'];
		
		$gps = new GPS($latVal,$longVal);*/
		
		$gps = new GPS($this->lat, $this->long);
		return $gps;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see src/Interfaces/XMLInterface#getElementStartTag()
	 */
	function getElementStartTag() {
		return $this->elementStart;	
	}
	
	/**
	 * (non-PHPdoc)
	 * @see src/Interfaces/XMLInterface#getElementEndTag()
	 */
	function getElementEndTag() {
		return $this->elementEnd;	
	}

}
?>
